﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmTimersTimer : Form
    {
        public FrmTimersTimer()
        {
            InitializeComponent();
        }
        System.Timers.Timer timer1 = null;
        System.Timers.Timer timer2 = null;
        int num = 1;
        int total = 0;
        private void FrmTimersTimer_Load(object sender, EventArgs e)
        {
            timer1 = new System.Timers.Timer();
            timer1.Interval = 1000; // 设置时间间隔
            timer1.AutoReset = true;//重复执行
            timer1.Elapsed += Timer1_Elapsed; // 绑定事件
            timer1.Start(); // 开始定时器

            timer2 = new System.Timers.Timer();
            timer2.Interval = 100;
            timer2.AutoReset = true;//重复执行
            timer2.Elapsed += Timer2_Elapsed;
            timer2.Start();
        }

        /// <summary>
        /// 从1加到100-计算结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer2_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (num <= 100) // 计算到100
            {
                total += num;
                this.Invoke(new Action(() =>
                {
                    lblCount.Text = num.ToString(); // 当前计算到多少
                    lblResult.Text = total.ToString(); // 计算结果
                }));
                num++;
            }
            else
                timer2.Stop();  // 计算完成停止计时器
        }

        /// <summary>
        /// 重复执行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //执行这里逻辑不是UI线程  ----子线程
            this.Invoke(new Action(() =>
            {
                lblCurTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//主线程执行
            }));
            Thread.Sleep(2000);
        }

        /// <summary>
        /// 窗体关闭前事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmTimersTimer_FormClosing(object sender, FormClosingEventArgs e)
        {   // 窗体关闭前停止time，否则会出现异常
            if (timer1.Enabled)
                timer1.Stop();
            if (timer2.Enabled)
                timer2.Stop();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmTimersTimer_FormClosed(object sender, FormClosedEventArgs e)
        {
           System.Environment.Exit(0);//终止此进程
        }

        /// <summary>
        /// 停止按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            timer1.Stop(); // 停止timer
        }
    }
}
